package simulator;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class GraphvizUtils {
	
	public void generateDotFile(Net net, String fileName) {
		File file = new File(fileName + ".dot");
		List<Pc> visited = new LinkedList<Pc>();
		try {
			BufferedWriter br = new BufferedWriter(new FileWriter(file));
			br.write("graph g{"+ '\n');
			for(Pc pc : net.getPcs()) {
				br.write("\"" + pc.getName() +"\" [label="+ "\""+ pc.getName() + "\" color=\"" + pc.getState().getColor() + "\"" +" style=\"filled\"]"+ "\n");
				for(Pc connectedPc: pc.getConnectedPcs()) {
					if(!visited.contains(connectedPc)) {
						br.write("\"" + pc.getName() + "\"--\"" + connectedPc.getName() + "\"\n");
					}
				}
				visited.add(pc);
			}
			br.write("}");
			br.close();
		} catch (IOException e) {
			System.out.println("ERROR: Problems with .dot file");
		}
		
	}
	public void generateDotFile(Net net, String fileName, Pc contagiada, Pc contagiante) {
		File file = new File(fileName + ".dot");
		List<Pc> visited = new LinkedList<Pc>();
		try {
			BufferedWriter br = new BufferedWriter(new FileWriter(file));
			br.write("graph g{"+ '\n');
			for(Pc pc : net.getPcs()) {
				br.write("\"" + pc.getName() +"\" [label="+ "\""+ pc.getName() + "\" color=\"" + pc.getState().getColor() + "\"" +" style=\"filled\"]"+ "\n");
				for(Pc connectedPc: pc.getConnectedPcs()) {
					if(!visited.contains(connectedPc)) {
						br.write("\"" + pc.getName() + "\"--\"" + connectedPc.getName() + "\" ");
						if(pc.equals(contagiada) && connectedPc.equals(contagiante) || pc.equals(contagiante) && connectedPc.equals(contagiada) ){
							br.write("[style=bold,color=red]");
						}
						br.write(" \n");
					}
				}
				visited.add(pc);
			}
			br.write("}");
			br.close();
		} catch (IOException e) {
			System.out.println("ERROR: Problems with .dot file");
		}
		
	}
//	dot -T jpg -o graph.jpg graph.dot
//	circo -T jpg -o graph.jpg graph.dot

}
